package com.calmcar.adas.service;

import com.calmcar.adas.apiserver.model.AdasPoint;
import com.calmcar.adas.apiserver.AdasConf;

import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;

/**
 * Created by lelexiao on 2018/5/5.
 */

public class FrameSplitProcess {

    public static Mat reSizeFrame(Mat mRgba ){
        int cropX=0,cropY=0,cropWidth=0,cropHeight=0;
         double  scale=1,scaleTwo=1;
        switch(AdasConf.WIDTH_HEIGHT){
            case "1920*1088":
                 scale=(double)1/2;
                 cropX=120;
                 cropY=2;
                 scaleTwo=(double)10/9;
                cropWidth=720;
                cropHeight=540;
                break;
            case "1600*1200":
                scale=(double)1/2;
                cropX=0;
                cropY=0;
                scaleTwo=1;
                cropWidth=800;
                cropHeight=600;
                break;
            case "1600*960":
                scale=(double)1/2;
                cropX=80;
                cropY=0;
                scaleTwo=(double)10/8;
                cropWidth=640;
                cropHeight=480;
                break;
            case "1440*1080":
                scale=(double)1/2;
                cropX=0;
                cropY=0;
                scaleTwo=(double)10/9;
                cropWidth=720;
                cropHeight=540;
                break;
            case "1440*960":
                scale=(double)1/2;
                cropX=40;
                cropY=0;
                scaleTwo=(double)10/8;
                cropWidth=640;
                cropHeight=480;
                break;
            case "1280*720":
                scale=1;
                cropX=160;
                cropY=0;
                scaleTwo=(double)10/12;
                cropWidth=960;
                cropHeight=720;
                break;
            case "1280*600":
                scale=1;
                cropX=240;
                cropY=0;
                scaleTwo=1;
                cropWidth=800;
                cropHeight=600;
                break;
            case "1280*480":
                scale=1;
                cropX=320;
                cropY=0;
                scaleTwo=(double)10/8;
                cropWidth=640;
                cropHeight=480;
                break;
            case "960*640":
                scale=1;
                cropX=80;
                cropY=20;
                scaleTwo=1;
                cropWidth=800;
                cropHeight=600;
                break;
            case "960*540":
                scale=1;
                cropX=120;
                cropY=0;
                scaleTwo=(double)10/9;
                cropWidth=720;
                cropHeight=540;
                break;
            case "864*480":
                scale=1;
                cropX=112;
                cropY=0;
                scaleTwo=(double)10/8;
                cropWidth=640;
                cropHeight=480;
                break;
            case "800*600":
                scale=1;
                cropX=0;
                cropY=0;
                scaleTwo=1;
                cropWidth=640;
                cropHeight=480;
                break;
            case "800*480":
                scale=1;
                cropX=80;
                cropY=0;
                scaleTwo=(double)10/8;
                cropWidth=640;
                cropHeight=480;
                break;
        }
        Rect rect = new Rect((int)(cropX/scale), (int)(cropY/scale), cropWidth,  cropHeight);
        Mat roi_img = new Mat(mRgba, rect);
        Mat tmp_img = new Mat();
        roi_img.copyTo(tmp_img);
        roi_img.release();
        Mat temp= new Mat();
        Imgproc.resize(tmp_img, temp, new Size(tmp_img.width()*scale*scaleTwo ,tmp_img.height()*scale*scaleTwo ));
        tmp_img.release();
        return  temp ;
    }

    public static AdasPoint resStoreSize(double x, double y ){
        int cropX=0,cropY=0,cropWidth=0,cropHeight=0;
        double  scale=1,scaleTwo=1;
        switch(AdasConf.WIDTH_HEIGHT){
            case "1920*1088":
                scale=(double)1/2;
                cropX=120;
                cropY=2;
                scaleTwo=(double)10/9;
                cropWidth=720;
                cropHeight=540;
                break;
            case "1600*1200":
                scale=(double)1/2;
                cropX=0;
                cropY=0;
                scaleTwo=1;
                cropWidth=800;
                cropHeight=600;
                break;
            case "1600*960":
                scale=(double)1/2;
                cropX=80;
                cropY=0;
                scaleTwo=(double)10/8;
                cropWidth=640;
                cropHeight=480;
                break;
            case "1440*1080":
                scale=(double)1/2;
                cropX=0;
                cropY=0;
                scaleTwo=(double)10/9;
                cropWidth=720;
                cropHeight=540;
                break;
            case "1440*960":
                scale=(double)1/2;
                cropX=40;
                cropY=0;
                scaleTwo=(double)10/8;
                cropWidth=640;
                cropHeight=480;
                break;
            case "1280*720":
                scale=1;
                cropX=160;
                cropY=0;
                scaleTwo=(double)10/12;
                cropWidth=960;
                cropHeight=720;
                break;
            case "1280*600":
                scale=1;
                cropX=240;
                cropY=0;
                scaleTwo=1;
                cropWidth=800;
                cropHeight=600;
                break;
            case "1280*480":
                scale=1;
                cropX=320;
                cropY=0;
                scaleTwo=(double)10/8;
                cropWidth=640;
                cropHeight=480;
                break;
            case "960*640":
                scale=1;
                cropX=80;
                cropY=20;
                scaleTwo=1;
                cropWidth=800;
                cropHeight=600;
                break;
            case "960*540":
                scale=1;
                cropX=120;
                cropY=0;
                scaleTwo=(double)10/9;
                cropWidth=720;
                cropHeight=540;
                break;
            case "864*480":
                scale=1;
                cropX=112;
                cropY=0;
                scaleTwo=(double)10/8;
                cropWidth=640;
                cropHeight=480;
                break;
            case "800*600":
                scale=1;
                cropX=0;
                cropY=0;
                scaleTwo=1;
                cropWidth=640;
                cropHeight=480;
                break;
            case "800*480":
                scale=1;
                cropX=80;
                cropY=0;
                scaleTwo=(double)10/8;
                cropWidth=640;
                cropHeight=480;
                break;
        }
            return  new AdasPoint(x/(scale*scaleTwo)+cropX/scale,y/(scale*scaleTwo)+cropY/scale) ;
    }

    public static void setROIRect(double x,double y,double width ,double height ){
        AdasConf. ROI_LEFT_X=x;
        AdasConf. ROI_LEFT_Y=y;
        AdasConf. ROI_WIDTH=width;
        AdasConf.ROI_HEIGHT=height;
    }


    public static Mat reSizeFrameForCarDetect(Mat mRgba ){//960*540
        Rect rect = new Rect(mRgba .width() / 3, mRgba .height() / 3, mRgba.width() / 3, mRgba.height() *5/ 8);
        setROIRect(mRgba .width() / 3, mRgba .height() / 3, mRgba.width() / 3, mRgba.height() *5 / 8);
        Mat roi_img = new Mat(mRgba, rect);
        Mat tmp_img = new Mat();
        roi_img.copyTo(tmp_img);
        Mat temp = new Mat();
        Imgproc.resize(tmp_img, temp, new Size(tmp_img.width()/ AdasConf.MAT_SCALE ,tmp_img.height()/ AdasConf.MAT_SCALE));
        return temp;
    }

    public static AdasPoint resStoreSizeForCarDetect(double x, double y){
        return  new AdasPoint(x* AdasConf.MAT_SCALE+ AdasConf.IN_FRAME_WIDTH/3,y* AdasConf.MAT_SCALE+ AdasConf.IN_FRAME_HEIGHT/3);
    }
}
